/**
 * 两个浮点数求和
 * @param {number|string} num1
 * @param {number|string} num2
 * @return {number}
 */
export function addOperation(num1, num2) {
	let r1, r2, m;
	try {
		r1 = (num1.toString().split(".")[1] || []).length;
	} catch (e) {
		r1 = 0;
	}
	try {
		r2 = (num2.toString().split(".")[1] || []).length;
	} catch (e) {
		r2 = 0;
	}
	m = Math.pow(10, Math.max(r1, r2));
	return Math.round(multiOperation(num1, m) + multiOperation(num2, m)) / m;
}

/**
 * 两个浮点数相减
 * @param {number|string} num1
 * @param {number|string} num2
 * @return {number}
 */
export function subOperation(num1, num2) {
	let r1, r2, m;
	try {
		r1 = (num1.toString().split(".")[1] || []).length;
	} catch (e) {
		r1 = 0;
	}
	try {
		r2 = num2.toString().split(".")[1].length;
	} catch (e) {
		r2 = 0;
	}
	m = Math.pow(10, Math.max(r1, r2));
	return Math.round(multiOperation(num1, m) - multiOperation(num2, m)) / m;
}

/**
 * 两个浮点数相乘
 * @param {number|string} num1
 * @param {number|string} num2
 * @return {number}
 */
export function multiOperation(num1, num2) {
	let m = 0;
	const s1 = num1.toString();
	const s2 = num2.toString();
	try {
		m += (s1.split(".")[1] || []).length;
	} catch (e) {}
	try {
		m += (s2.split(".")[1] || []).length;
	} catch (e) {}
	return (Number(s1.replace(".", "")) * Number(s2.replace(".", ""))) / Math.pow(10, m);
}

/**
 * 两个浮点数相除
 * @param {number|string} num1
 * @param {number|string} num2
 * @return {number}
 */
export function divideOperation(num1, num2) {
	let m = 0;
	const s1 = num1.toString();
	const s2 = num2.toString();
	try {
		m += (s1.split(".")[1] || []).length;
	} catch (e) {}
	try {
		m += (s2.split(".")[1] || []).length;
	} catch (e) {}
	const n1 = multiOperation(num1, Math.pow(10, m));
	const n2 = multiOperation(num2, Math.pow(10, m));
	return Number(n1) / Number(n2);
}
